#include <stdio.h>
#include <string.h>

#include "../contrib/libsmf/smf.h"

#include "B3_defines.h"
#include "B3_consts.h"
#include "welltempMore.h"
#include "drawbar.h"
#include "gentone.h"
#include "playnote.h"
#include "save_wav.h"
#include "filters.h"
#include "effect.h"
#include "init_args.h"  // this is not a normal head file.
#include "midi_pp.h"

//temporary usage: mid2wav midi_file wave_file drawbar_setting1 drawbar_setting2
int
main(int argc, char *argv[]) {
        if (argc != 5) {
                printf("Usage: mid2wav midi_file wave_file drawbar_setting1 drawbar_setting2\n");
                return 1;
        }
        //in case when we change the arguments.
        char *midi_file = argv[1];
        char *wave_file = argv[2];
        char *drawbar_setting1 = argv[3];
        char *drawbar_setting2 = argv[4];

        smf_t *smf;
        smf_event_t *event;
	B3_event_t *B3_events;
        unsigned char* buf;
        //int sample_rate;

        init_args(); // from init_args.h

        if (strlen(drawbar_setting1) != 9 ||
                        strlen(drawbar_setting2) != 9 ||
                        strlen(drawbar_setting1) != strspn(drawbar_setting1, "012345678") ||
                        strlen(drawbar_setting2) != strspn(drawbar_setting2, "012345678")) {
                printf("wrong drawbar_setting\n");
                return 1;
        }
        pull_drawbar_from_str(drawbar1, drawbar_setting1); // from drawbar.h
        pull_drawbar_from_str(drawbar2, drawbar_setting2);

        //TODO: get sample rate and filename from args
        //done: get filename and drawbar setting from args. sample rate is decided by the wave table.
        //sample_rate = 192000;
        smf = smf_load(midi_file);
        if (smf == NULL) {
                printf("cannot open mid file\n");
                return 1;
        }
	//FIXME for test
	B3_events = midi_preprocess(smf);
	free(B3_events);
	smf_rewind(smf);
        //TODO: get drawbar settings
        //done: from args

        //TODO: init tone wheel level array to 0

        double last_time = 0.0;
        long wav_buf_pos = 0;

        while ((event = smf_get_next_event(smf)) != NULL) {
                buf = smf_event_decode_B3(event);
                if (buf == NULL)
                        continue;
                printf("%f, %d, %d, %d, %d\n", event->time_seconds, buf[0], buf[1], buf[2], buf[3]);
                long last_size = SAMPLE_RATE * (event->time_seconds - last_time);
                gentone(last_size, &(wav_buf[wav_buf_pos]));
                wav_buf_pos += last_size;
                last_time = event -> time_seconds;

                if (buf[1] == 1)
                        keys1[ buf[2] - 35 ] = (int)(buf[0] && (buf[3] > 0)); //mid format!! // mid C is note 60, key 25. 60 - 25 = 35
                else if (buf[1] == 2)
                        keys2[ buf[2] - 35 ] = (int)(buf[0] && (buf[3] > 0)); //some software doesn't generate noteoff, some software doesn't generate velocity 0...
                calculate_wheel_v();


                free(buf);
        }
        //output_lp_filter(wav_buf, wav_buf_pos);
        wav_buf_pos = dodo_vib_chorus(412.0/60, 120.0, 0.5 ,wav_buf, wav_buf_pos); //412 rpm from hammondwiki
        save_wav(wave_file, wav_buf_pos, wav_buf, 1, SAMPLE_RATE);


        return 0;
}
